package yaq.mini.weibo.leafwb.service;

import org.springframework.stereotype.Service;
import yaq.mini.weibo.leafwb.mapper.ArticleMapper;
import yaq.mini.weibo.leafwb.mapper.CommentMapper;
import yaq.mini.weibo.leafwb.pojo.Comment;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ArticleMapper articleMapper;

    public boolean insertByComment(Comment comment) {
        try {
            commentMapper.insert(comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public List<Comment> listComment(String articleId) {
        List<Comment> commentList;
        try {
            commentList = commentMapper.getListCommentByAID(articleId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return commentList;
    }

    public boolean deleteComment(String commentId) {
        try {
            Comment comment = commentMapper.getCommentByID(commentId);
            System.out.println("2:"+comment.getArticleId());
            articleMapper.updateByCommentCount(comment.getArticleId(),-1);
            commentMapper.deleteByID(commentId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
